<?php
/**
*
*	图片处理函数库 @zairwolf
*
*/

if(!defined('IN')) exit('Access Denied');

//生成VIP章节图片
function MakeImage($content,$filename){
	global $_SYSTEM;
	include _config('read');
	//$content=iconv('gb2312','UTF-8',$content);
	$font="data/font/".$_SYSTEM['READ']['font'];

	//内容排版
	$content=explode("\n",$content);
	$newstr='';
	foreach($content as $val){
		if(!trim($val)) continue;
		$val="    ".trim($val);
		$newstr.=implode("\n",mb_str_split($val, $_SYSTEM['READ']['line']))."\n".str_repeat("\n",$_SYSTEM['READ']['brnum']);
	}
	$fontposarr=ImageTTFBBox($_SYSTEM['READ']['size'],0,$font,$newstr);
	$xlen=$fontposarr[4]-$fontposarr[6];//得到行宽
	$linearr=ImageTTFBBox($_SYSTEM['READ']['size'],0,$font,$newstr[0]);
	$pylen=$linearr[1]-$linearr[7];//单行高 增加间距可控 2009-5-13
	$lineArr=explode("\n",$newstr);
	$lineNum=count($lineArr);
	$lineTotalHeight=$pylen+$_SYSTEM['READ']['lineheight'];
	$ylen=$lineNum*$lineTotalHeight;

	//开始生成
	$im = ImageCreate($xlen+35,$ylen+50);
	$_SYSTEM['READ']['background']=explode(" ",$_SYSTEM['READ']['background']);
	ImageColorAllocate($im, $_SYSTEM['READ']['background'][0],$_SYSTEM['READ']['background'][1],$_SYSTEM['READ']['background'][2]);//背景

	//写入网站网址
	$copyright=str_replace('http://','',$_SYSTEM['SYSTEM']['SITE_ADDR']);
	$font2="data/font/".$_SYSTEM['READ']['siteurl_font'];
	$color2=explode(" ",$_SYSTEM['READ']['siteurl_color']);
	$color2 = ImageColorAllocate($im, $color2[0],$color2[1],$color2[2]);
	for($ii=1; $ii<=$_SYSTEM['READ']['siteurl_repeat']; $ii++){
		ImageTTFText($im, $_SYSTEM['READ']['siteurl_size'], $_SYSTEM['READ']['siteurl_angle'], $xlen/3, $ylen*$ii/$_SYSTEM['READ']['siteurl_repeat'], $color2, $font2, $copyright);
	}
	//ImageString($im,5,$xlen-strlen($copyright)*10,$ylen-20,$copyright,$color2);

	//写入章节内容
	$_SYSTEM['READ']['color']=explode(" ",$_SYSTEM['READ']['color']);
	$color = ImageColorAllocate($im, $_SYSTEM['READ']['color'][0],$_SYSTEM['READ']['color'][1],$_SYSTEM['READ']['color'][2]);//文字颜色

	//写入文字
	foreach($lineArr as $key=>$val){
		ImageTTFText($im, $_SYSTEM['READ']['size'], 0, 10, 20+$key*$lineTotalHeight, $color, $font, $val);
	}
	//ImageTTFText($im, $_SYSTEM['READ']['size'], 0, 10, 20, $color, $font, $newstr);


	ImageJPEG($im,$filename);
	ImageDestroy($im);
}

//增加用户编号水印
function AddUserMark($pic) {
	global $_G;
	include _config('read');
	header("Content-type: image/jpeg");
	$copyright=str_replace("[rd8vip]",$_G['user_name'],$_SYSTEM['READ']['copyright']);

	list($width, $height) = GetImageSize($pic);
	$im=ImageCreateFromJPEG($pic);
	$gray=ImageColorAllocate($im,225,225,225);
	$black=ImageColorAllocate($im,255,255,255);

	//写入uid
	if($_G['user_id']){
		//写入欢迎&vip版权
		$font="data/font/".$_SYSTEM['READ']['copyright_font'];
		$fontposarr=ImageTTFBBox($_SYSTEM['READ']['copyright_size'],0, $font, $copyright);
		$xlen=$fontposarr[4]-$fontposarr[6];
		$color=explode(" ",$_SYSTEM['READ']['copyright_color']);
		$color = ImageColorAllocate($im, $color[0],$color[1],$color[2]);
		ImageTTFText($im, $_SYSTEM['READ']['copyright_size'], 0, $width-$xlen, $height-40, $color, $font, $copyright);


		$font="data/font/".$_SYSTEM['READ']['mark_font'];
		$color=explode(" ",$_SYSTEM['READ']['mark_color']);
		$color = ImageColorAllocate($im, $color[0],$color[1],$color[2]);
		ImageTTFText($im, $_SYSTEM['READ']['mark_size'], 0, $width*3/5, rand($height/10,$height), $color, $font, $_G['user_id']);
		ImageTTFText($im, $_SYSTEM['READ']['mark_size'], 0, $width*2/5, rand($height/10,$height), $color, $font, $_G['user_id']);//防盗位置更改
//		for($ii=1; $ii<10; $ii++){
//			$tmpheight=$height*$ii/10;
//		}
	}
	ImageJPEG($im);
	ImageDestroy($im);
	exit();
}

function ImageWaterMarkImage($src, $pos, $w, $a){
	$im = ImageCreateFromJPEG($src);
	$wa = ImageCreateFromPNG($w);

	$swh = GetImageSize($src);
	$twh = GetImageSize($w);
	switch($pos) {
		case 1:
			$x = 8;
			$y = 8;
			break;
		case 2:
			$x = ($swh[0] - $twh[0]) / 2;
			$y = 8;
			break;
		case 3:
			$x = $swh[0] - $twh[0] - 8;
			$y = 8;
			break;
		case 4:
			$x = 8;
			$y = ($swh[1] - $twh[1]) / 2;
			break;
		case 5:
			$x = ($swh[0] - $twh[0]) / 2;
			$y = ($swh[1] - $twh[1]) / 2;
			break;
		case 6:
			$x = $swh[0] - $twh[0] - 8;
			$y = ($swh[1] - $twh[1]) / 2;
			break;
		case 7:
			$x = 8;
			$y = $swh[1] - $twh[1] - 8;
			break;
		case 8:
			$x = ($swh[0] - $twh[0]) / 2;
			$y = $swh[1] - $twh[1] - 8;
			break;
		case 9:
			$x = $swh[0] - $twh[0] - 8;
			$y = $swh[1] - $twh[1] - 8;
			break;
	}

	ImageCopyMerge($im, $wa, $x, $y, 0, 0, $twh[0], $twh[1], $a);
	ImageJPEG($im, $src);
}

function ImageWaterMarkText($src, $pos, $t, $c){
	$im = ImageCreateFromJPEG($src);

	list($r, $g, $b) = explode(' ', $c);
	$co = ImageColorAllocate($im, $r, $g, $b);

	$swh = GetImageSize($src);
	$twh = array(strlen($t) * 8, 15);
	switch($pos) {
		case 1:
			$x = 8;
			$y = 8;
			break;
		case 2:
			$x = ($swh[0] - $twh[0]) / 2;
			$y = 8;
			break;
		case 3:
			$x = $swh[0] - $twh[0] - 8;
			$y = 8;
			break;
		case 4:
			$x = 8;
			$y = ($swh[1] - $twh[1]) / 2;
			break;
		case 5:
			$x = ($swh[0] - $twh[0]) / 2;
			$y = ($swh[1] - $twh[1]) / 2;
			break;
		case 6:
			$x = $swh[0] - $twh[0] - 8;
			$y = ($swh[1] - $twh[1]) / 2;
			break;
		case 7:
			$x = 8;
			$y = $swh[1] - $twh[1] - 8;
			break;
		case 8:
			$x = ($swh[0] - $twh[0]) / 2;
			$y = $swh[1] - $twh[1] - 8;
			break;
		case 9:
			$x = $swh[0] - $twh[0] - 8;
			$y = $swh[1] - $twh[1] - 8;
			break;
	}

	ImageString($im, 4, $x, $y, $t, $co);

	ImageJPEG($im, $src);
}


//内容分段函数
function mb_str_split($string, $split_length, $encoding = 'UTF-8') {
	$string = convert($string, 'utf-8', 'gbk');

	mb_internal_encoding('gbk');
	$totlen = strlen($string);
	$offset = 0;
	$return = array();
	while(strlen($string)) {
		$length = $split_length * 2;
		if(strlen($string) < $length)
			$length = strlen($string);

		$offset = $split_length;
		$return[] = convert(mb_strcut($string, 0, $offset), 'gbk', 'utf-8');
		$string = mb_strcut($string, $offset);
	}
	return $return;
}


function GetImageType($filename){
	if(function_exists('exif_imagetype')) {
		$imagetype = exif_imagetype($filename);
	}else {
		$info = GetImageSize($filename);
		if(false === $info)
			return false;

		$imagetype = $info[2];
	}
	$ext = GetImageExt($imagetype);
	if(false === $ext)
		return false;

	return array($ext, GetImageMime($imagetype));
}

function GetImageExt($imagetype){
	switch($imagetype) {
		case 1:
			return 'gif';
		case 2:
			return 'jpg';
		case 3:
			return 'png';
		case 6:
			return 'bmp';
	}
	return false;
}

function GetImageMime($imagetype){
	if(function_exists('image_type_to_mime_type'))
		return image_type_to_mime_type($imagetype);

	switch($imagetype) {
		case 1:
			return 'image/gif';
		case 2:
			return 'image/jpeg';
		case 3:
			return 'image/png';
		case 4:
		case 13:
			return 'application/x-shockwave-flash';
		case 5:
			return 'image/psd';
		case 6:
			return 'image/bmp';
		case 7:
		case 8:
			return 'image/tiff';
		case 9:
		case 11:
		case 12:
			return 'application/octet-stream';
		case 10:
			return 'image/jp2';
		case 14:
			return 'image/iff';
		case 15:
			return 'image/vnd.wap.wbmp';
		case 16:
			return 'image/xbm';
	}
	return false;
}



